package de.lot.action;

import de.lot.model.LearningOutcome;
import de.lot.model.ProgramGoal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.framework.EntityHome;

/**
 * Class to interact between front- and backend for the ProgramGoal
 *
 * @see ProgramGoal
 * @author Stefan Kohler <kohler.stefan@gmail.com>
 */
@Name("programGoalHome")
public class ProgramGoalHome extends EntityHome<ProgramGoal> {

    @RequestParameter
    Long programGoalId;

    @Override
    public Object getId() {
        if (programGoalId == null) {
            return super.getId();
        } else {
            return programGoalId;
        }
    }

    public void setProgramGoalId(Long id) {
        setId(id);
    }

    public Long getProgramGoalId() {
        return (Long) getId();
    }

    @Override
    @Begin(join = true)
    public void create() {
        super.create();
    }

    public ProgramGoal getDefinedInstance() {
        return isIdDefined() ? getInstance() : null;
    }

    public void wire() {
        getInstance();
    }

    public boolean isWired() {
        return true;
    }

    /**
     * A list of LearningOutcomes sorted by the number
     * @return List<LearningOutcome> learningOutcomes
     */
    public List<LearningOutcome> getLearningOutcomes() {
        if (getInstance() == null) {
            return null;
        }
        List<LearningOutcome> learningOutcomes = new ArrayList<LearningOutcome>(getInstance().getLearningOutcomeList());
        Collections.sort(learningOutcomes, new Comparator<LearningOutcome>() {

            public int compare(LearningOutcome a, LearningOutcome b) {
                return Integer.valueOf(a.getNumber().compareTo(Integer.valueOf(b.getNumber())));
            }
        });
        return learningOutcomes;
    }
}
